package pl.game.map;

import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import pl.game.MapView;
import pl.game.R;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

@Root
public class Map {
	
	@Element
	private String name;
	
	@Element 
	private String bitmapString;
	
	private int bitmap;
	
	@ElementList
	private List<Tile> tileList;
	
	
	public Map(){}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getBitmapString() {
		return bitmapString;
	}

	public void setBitmapString(String bitmapString) {
		this.bitmapString = bitmapString;
	}

	public int getBitmap() {
		return bitmap;
	}

	public void setBitmap(int bitmap) {
		this.bitmap = bitmap;
	}

	public List<Tile> getTileList() {
		return tileList;
	}
	public void setTileList(List<Tile> tileList) {
		this.tileList = tileList;
	}
	
	public void decodeTiles(MapView mapView) {
		
		Class c = R.drawable.class;
		int i = 0;

		try 
		{
			this.bitmap = c.getField(this.getBitmapString()).getInt(this);
			for (Tile tile : tileList) {

				i = c.getField(tile.getFlagBitmap()).getInt(tile);
				tile.setBitmap(BitmapFactory.decodeResource(
						mapView.getResources(), i));

				tile.setMapView(mapView);
				tile.setCaptured(false);
				tile.setClicked(false);
				tile.setNeighbors(new ArrayList<Tile>());

				tile.setWidth(tile.getBitmap().getWidth());
				tile.setHeight(tile.getBitmap().getHeight());

				for (Integer neighbour : tile.getNeighbours()) {

					tile.addNeighbor(tileList.get(neighbour));
				}
			}

		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	

}
